﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Vibz.TeamAssignment.Common.Entity;

namespace Vibz.TeamAssignment
{
    public partial class FlashAlertForm :Form
    {
        bool _cancelClose = false;
        protected override bool ShowWithoutActivation
        {
            get
            {
                return true;
            }
        }
        public FlashAlertForm(Dictionary<TaskView, DeltaChange> delta)
        {
            InitializeComponent();
            ShowMessage(delta);
        }
        void ShowMessage(Dictionary<TaskView, DeltaChange> delta)
        {
            rtbMessage.Text = "";
            foreach(TaskView tv in delta.Keys)
            {
                switch(delta[tv])
                {
                    case DeltaChange.New:
                        rtbMessage.Text += "\r\n\tNew: " + tv.Task.Title;
                        rtbMessage.Text += "\r\n\t   By: " + tv.LatestAssignment.AssignBy;
                        rtbMessage.Text += "\r\n\t   Due Date: " + tv.Task.DueDate.ToShortDateString();
                        break;
                    case DeltaChange.Removed:
                        rtbMessage.Text += "\r\n\tRemoved: " + tv.Task.Title;
                        rtbMessage.Text += "\r\n\t   Comment: " + tv.LatestAssignment.AssignComment;
                        break;
                    case DeltaChange.Update:
                        rtbMessage.Text += "\r\n\tUpdate: " + tv.Task.Title;
                        rtbMessage.Text += "\r\n\t   By: " + tv.LatestAssignment.AssignBy;
                        rtbMessage.Text += "\r\n\t   Due Date: " + tv.Task.DueDate.ToShortDateString();
                        break;
                }
            }
        }
        private void FlashAlertForm_Load(object sender, EventArgs e)
        {
            fade_in();
        }
        private void FlashAlertForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            fade_out();
            if(_cancelClose)
            {
                e.Cancel = true;
                this.Opacity = 1;
            }
        }
        public void fade_in()
        {
            for(double FadeIn = 5; FadeIn < 100; FadeIn += 5)
            {
                this.Opacity = FadeIn / 100;
                this.Refresh();
                System.Threading.Thread.Sleep(300);
            }
        }
        public void fade_out()
        {
            for(double FadeOut = 100; FadeOut > 5; FadeOut += -1)
            {
                if(_cancelClose)
                    break;
                this.Opacity = FadeOut / 100;
                this.Refresh();
                System.Threading.Thread.Sleep(100);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            this.Owner.MaximizeBox = true;
        }

        private void FlashAlertForm_MouseEnter(object sender, EventArgs e)
        {
            timer1.Stop();
            _cancelClose = true;
        }

        private void FlashAlertForm_MouseLeave(object sender, EventArgs e)
        {
            timer1.Start();
            _cancelClose = false;
        }
    }
}
